package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.repairwork;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.business.bean.PostDiagnoseWrapperBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-11 11:35
 */

public class RepairWorkModel implements RepairWorkContract.Model {

    @Override
    public Disposable deleteDiagnose(String token, int repairId, int faultDiagnosticId,
                                     int faultDiagnosticIdType,
                                     final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.DeleteDiagnostic)
                .params("repairId", repairId)
                .params("token", token)
                .params("faultDiagnosticId", faultDiagnosticId)
                .params("faultDiagnosticIdType", faultDiagnosticIdType)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable postDiagnoseList(String token, int repairId, int repairTaskId,
                                       int vehicleSeriesId,
                                       String faultDiagnosticList,
                                       final OnDataConvertListener<PostDiagnoseWrapperBean> listener) {
        return RxRestClient.builder()
                .url(Url.ConfirmDiagnose)
                .params("repairId", repairId)
                .params("token", token)
                .params("repairTaskId", repairTaskId)
                .params("faultDiagnosticList", faultDiagnosticList)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertDiagnoseList(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable deleteProgram(String token, int repairId,
                                    int faultDiagnosticId, int planId,
                                    final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.DeleteProgram)
                .params("repairId", repairId)
                .params("token", token)
                .params("diagnosticId", faultDiagnosticId)
                .params("planId", planId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable returnGoods(String token, String items,
                                  int planId, final OnDataConvertListener<String> returnListener) {
        return RxRestClient.builder()
                .url(Url.MaterialReturn)
                .params("token", token)
                .params("planId", planId)
                .params("items", items)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        returnListener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        returnListener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable addCoWorker(String token, int id, final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.AddCoWorker)
                .params("token", token)
                .params("sceneRepairPlanId", id)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable cancelCoWorker(String token, int id, final OnDataConvertListener<String> listener) {
        return RxRestClient.builder()
                .url(Url.CancelCoWorker)
                .params("token", token)
                .params("sceneRepairPlanId", id)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    @Override
    public Disposable addTempLocation(String token, String tempLocation, final OnDataConvertListener<List<PartLocationBean>> listener) {
        return RxRestClient.builder()
                .url(Url.AddTempLocation)
                .params("token", token)
                .params("positionList", tempLocation)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertPartLocation(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private PostDiagnoseWrapperBean convertDiagnoseList(String response) {
        return new ObjConverter<PostDiagnoseWrapperBean>().convert(response
                , new TypeReference<PostDiagnoseWrapperBean>() {
                });
    }

    private List<PartLocationBean> convertPartLocation(String response) {
        return new ObjConverter<List<PartLocationBean>>().convert(response
                , new TypeReference<List<PartLocationBean>>() {
                });
    }
}
